//
//  CJExten+String.swift
//  CJCore
//
//  Created by 陈警卫 on 2024-06-14.
//

import UIKit
import Foundation

public extension String {
    
    /// 转Class
    func cj_class() -> AnyClass? {
        let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        guard let Class = NSClassFromString(namespace.replacingOccurrences(of: " ", with: "_") + "." + self) else {
            return nil
        }
        return Class
    }
    
    /// 转Class
    func cj_class_oc() -> AnyClass? {
        guard let Class = NSClassFromString(self) else {
            return nil
        }
        return Class
    }
    
    /// 判断是否包含 ignoringCase 忽略大小写
    func cj_contains(sub: String, ignoringCase: Bool = false) -> Bool {
        var options: String.CompareOptions = []
        if ignoringCase {
            options = .caseInsensitive
        }
        if self.range(of: sub, options: options) != nil {
            return true
        }
        return false
    }
    
    /// 根据字符进行分隔
    func cj_separatedBy(char: CharacterSet) -> [String] {
        let value = self.components(separatedBy: char)
        return value
    }
    
}

public extension String {
    
    /// 取字符串
    func cj_sub(start: Int, end: Int) -> String {
        let sIndex = index(startIndex, offsetBy: start)
        let eIndex = index(startIndex, offsetBy: end)
        if end >= self.count {
            return String()
        }
        return String(self[sIndex..<eIndex])
    }
    
    /// 取字符串
    func cj_sub(loc: Int, len: Int) -> String {
        let start = loc
        let end = loc + len
        let value = cj_sub(start: start, end: end)
        return value
    }
    
    /// 取字符串
    func cj_sub(to index: Int) -> String {
        let value = cj_sub(start: 0, end: index)
        return value
    }
    
    /// 取字符串
    func cj_sub(from index: Int) -> String {
        let value = cj_sub(start: index, end: (self.count - 1))
        return value
    }
    
    /// 取字符串
    func cj_index(loc: Int) -> String {
        let value = cj_sub(loc: loc, len: 1)
        return value
    }
    
}

public extension String {
    
    /// 复制
    func cj_copy() {
        UIPasteboard.general.string = self
    }
    
    /// 随机数
    static func cj_randomString() -> String {
        let random = arc4random() % 1000000000
        return String(format: "%06ld", random)
    }
}

public extension String {
    
    /// 时间format字符串转Date
    func cj_date(format: String = "YYYY-MM-dd HH:mm:ss", timeZone: TimeZone?, locale: Locale?) -> Date {
        let dateFormat = DateFormatter()
        dateFormat.dateFormat = format
        if let timeZone = timeZone {
            dateFormat.timeZone = timeZone
        }
        if let locale = locale {
            dateFormat.locale = locale
        }
        let date = dateFormat.date(from: self) ?? Date()
        return date
    }
    
    /// 时间戳转Date
    var cj_stampToDate: Date {
        let inter = self.cj_int
        var interval = Double(inter)
        // 时间戳 10位 13位
        interval = String(inter).count == 13 ? interval/1000 : interval
        let date = Date(timeIntervalSince1970: interval)
        return date
    }
    
    /// 时间戳转DateFormat字符串 
    /// format默认 "YYYY-MM-dd HH:mm:ss"
    func cj_dateString(format: String = "YYYY-MM-dd HH:mm:ss") -> String {
        let date = cj_stampToDate
        let dateFormat = DateFormatter()
        dateFormat.dateFormat = format
        let dateString = dateFormat.string(from: date)
        return dateString
    }
    
    /// 时间戳转时钟 00:00:00
    var cj_clock: String {
        let seconds = self.cj_intRound
        let hour = String(format: "%02ld", seconds/3600)
        let minute = String(format: "%02ld", (seconds%3600)/60)
        let second = String(format: "%02ld", seconds%60)
        let clock = hour + ":" + minute + ":" + second
        return clock
    }
    
    /// 时间戳转时钟 00:00/00:00:00
    var cj_clockFits: String {
        let seconds = self.cj_intRound
        if seconds < 3600 {
            let minute = String(format: "%02ld", (seconds%3600)/60)
            let second = String(format: "%02ld", seconds%60)
            let clock = minute + ":" + second
            return clock
        } else {
            return cj_clock
        }
    }
}
